﻿#region Using Statements
using System;
using GameBase.Screens;
using GameBase.Sprites;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using StarFox.Sounds;
#endregion

namespace StarFox.ScreenComponents
{
    /// <summary>
    /// Messages associated to GamePlayScreen (warning).
    /// </summary>
    public class WarningMessage : ScreenComponent2D
    {
        #region Fields

        SpriteSheet spriteSheet;
        GamePlaySound sound;

        float warningMsgTime;
        bool warningMsgBlink;

        #endregion

        #region Properties

        /// <summary>
        /// Sets the duration of the message.
        /// </summary>
        public TimeSpan Duration
        {
            private get;
            set;
        }

        #endregion

        #region Screen Transitions

        /// <summary>
        /// Makes the component transition to visible state.
        /// </summary>
        public override void Show()
        {
        }

        /// <summary>
        /// Makes the component transition to hidden state.
        /// </summary>
        public override void Hide()
        {
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of this class.
        /// </summary>
        /// <param name="screen">The Screen that this ScreenComponent2D is associated to.</param>
        public WarningMessage(Screen screen)
            : base(screen)
        {
            spriteSheet = Game.Content.Load<SpriteSheet>(ContentSettings.GetAsset("MSG_SPRITES"));

            sound = Game.Services.GetService(typeof(GamePlaySound)) as GamePlaySound;
            if (sound == null)
                throw new InvalidOperationException("Service of type StarFoxSound not found");
        }

        #endregion

        #region Warning - LowHealth

        bool isLowHealth;
        bool initLowHealth;

        /// <summary>
        /// Shows whether the StarShip's health level is too low.
        /// </summary>
        /// <param name="health">StarShip's health.</param>
        public void LowHealth(float health)
        {
            isLowHealth = true;

            if (!initLowHealth)
            {
                initLowHealth = true;
                warningMsgTime = 0.0f;
                warningMsgBlink = true;
            }

            sound.Danger_LowHealth(0.50f - 1.5f * (health - 0.10f));
        }

        #endregion

        #region Warning - OutFuel

        bool isOutFuel;
        bool initOutFuel;

        /// <summary>
        /// Shows whether the StarShip's fuel level is too low.
        /// </summary>
        public void OutFuel()
        {
            isOutFuel = true;

            if (!initOutFuel)
            {
                initOutFuel = true;
                warningMsgTime = 0.0f;
                warningMsgBlink = true;
            }

            sound.Danger_OutFuel();
        }

        #endregion

        #region Warning - OutBoost

        bool isOutBoost;

        /// <summary>
        /// Shows whether the StarShip's boost level is too low.
        /// </summary>
        public void OutBoost()
        {
            isOutBoost = true;
        }

        #endregion

        #region Update

        /// <summary>
        /// Called when the ScreenComponent2D needs to be updated.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Update(GameTime gameTime)
        {
            if (isLowHealth || isOutFuel)
            {
                float timeDelta = (float)(gameTime.ElapsedGameTime.TotalMilliseconds / Duration.TotalMilliseconds);
                warningMsgTime += timeDelta;
                warningMsgTime = MathHelper.Clamp(warningMsgTime, 0.0f, 1.0f);
                if (warningMsgTime == 1.0f) { warningMsgTime = 0.0f; warningMsgBlink = !warningMsgBlink; }
            }
            else
            {
                initLowHealth = false;
                initOutFuel = false;
            }
        }

        #endregion

        #region Draw

        /// <summary>
        /// Called when the ScreenComponent2D needs to be drawn.
        /// </summary>
        /// <param name="gameTime">Time elapsed since the last call to this method.</param>
        public override void Draw(GameTime gameTime)
        {
            if (isLowHealth)
            {
                if (warningMsgBlink)
                {
                    Rectangle auxRect = spriteSheet.SourceRectangle("LOWHEALTH");
                    SpriteBatch.Draw(spriteSheet.Texture, new Vector2(Screen.Width * 0.5f, Screen.Height * 0.75f), auxRect, Color.Yellow,
                                     0.0f, new Vector2(auxRect.Width / 2, auxRect.Height / 2), Screen.Scale, SpriteEffects.None, 0);
                }
            }
            else if (isOutFuel)
            {
                if (warningMsgBlink)
                {
                    Rectangle auxRect = spriteSheet.SourceRectangle("OUTFUEL");
                    SpriteBatch.Draw(spriteSheet.Texture, new Vector2(Screen.Width * 0.5f, Screen.Height * 0.75f), auxRect, Color.Yellow,
                                     0.0f, new Vector2(auxRect.Width / 2, auxRect.Height / 2), Screen.Scale, SpriteEffects.None, 0);
                }
            }
            else if (isOutBoost)
            {
                Rectangle auxRect = spriteSheet.SourceRectangle("OUTBOOST");
                SpriteBatch.Draw(spriteSheet.Texture, new Vector2(Screen.Width * 0.5f, Screen.Height * 0.75f), auxRect, Color.Yellow,
                                 0.0f, new Vector2(auxRect.Width / 2, auxRect.Height / 2), Screen.Scale, SpriteEffects.None, 0);
            }

            // reset flags every frame
            isLowHealth = false;
            isOutFuel = false;
            isOutBoost = false;
        }

        #endregion
    }
}
